package com.base.cn.platform.os.service.course.cou;

import com.base.cn.platform.os.common.mybatis.BaseBiz;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.dao.course.cou.CourseProfileDao;
import com.base.cn.platform.os.entity.course.cou.Course;
import com.base.cn.platform.os.entity.course.cou.CourseProfile;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 课程扩展统计数据
 *
 * @author s.li
 * @create 2018-05-23-10:40
 */
@Service
public class CourseProfileBiz extends BaseBiz<CourseProfile,CourseProfileDao> {

    /**
     * 创建课程扩展数据
     * @param courseId 课程ID
     */
    public void createCourseProfile(BigDecimal courseId){
        CourseProfile profile = new CourseProfile();
        profile.setId(courseId);
        this.save(profile);
    }

    /**
     * 设置课程Profile数据
     * @param course 课程数据
     * @param setProfileData
     * @return Course
     */
    public Course setCourseProfileData(Course course,boolean setProfileData){
        if(course!=null && setProfileData){
            List<Course> courseList = new ArrayList<>();
            courseList.add(course);
            courseList = setCourseProfileData(courseList,setProfileData);
            course = courseList.get(0);
        }
        return course;
    }


    /**
     * 设置课程Profile数据
     * @param courseList 课程数据列表
     * @param setProfileData
     * @return List<Course>
     */
    public List<Course> setCourseProfileData(List<Course> courseList,boolean setProfileData){
        if(ObjectUtils.isNotEmpty(courseList) && setProfileData){
            String courseIds = courseList.stream().map(e->e.getId().toString()).collect(Collectors.joining(","));
            List<CourseProfile> profileList = this.find("id in ("+courseIds+")",null,null);
            if(ObjectUtils.isNotEmpty(profileList)){
                Map<BigDecimal,CourseProfile> courseProfileMap = profileList.stream().collect(Collectors.toMap(e->e.getId(),e->e));
                courseList.forEach(course->{
                    CourseProfile profile = courseProfileMap.get(course.getId());
                    course.setCourseProfile(profile);
                });
            }
        }
        return courseList;
    }
}
